function [sigma,d1,v1,rcoun]=drawscon(erdata,d0,v0,cind,conv);
%function [sigma,d1,v1,rcoun]=drawscon(erdata,d0,v0,cind,conv); 
%
% Draws from the posterior of a  Variance 
% subject to constraints and rejection sampling 
% when the prior for the precision (1/sigma^2) is given by 
% Gamma (d0/2,2/v0)
% cond is an indicator of the constraint 1 is greater than 
%                                       -1 is less than 
% conv  constraint vector 
% e.g cind=1 conv=1.5 
% samples with rejection until the draw is greater than 1.5
%
% Posterior is: 
% sigma^2 ~ InverseGamma ([do+Ty]/2 , [vo+erdata'erdata]/2 )
%
% sigma     sampled variance 
% d1        posterior degrees of freedom 
% v1        posterior scale parameter 
%
% Using integer values for d0 results in faster sampling 
%
% Modified 12/10/03
d1= d0 + length(erdata) ;  
v1=( (erdata')*erdata ) +v0; 

if ( floor(d1) - d1 ) == 0
    eas=1; 
else 
    eas=0
end

acc=0; 
rcoun=0; 
while acc < 1; 
    if eas == 1
        nor=randn(d1,1); 
        chi=nor'*nor; 
        sigma=v1/chi; 
    else 
        sigma=1./gamrnd(d1/2,2/v1,1,1) 
    end 
    if cind*sigma > cind*conv 
        acc=1; 
    end 
    rcoun=rcoun+1; 
    if rem(rcoun,2000) == 0; 
        warning('2000 draws have not yet satisifed the constraint in drawscon.m') 
    end 
end 
rcoun=rcoun-1; 



